package com.tanhua.log.service;

import com.tanhua.dubbo.server.pojo.UserFreeze;
import com.tanhua.log.domain.Log;
import com.tanhua.log.vo.LoginLogAllVo;
import com.tanhua.log.vo.LoginLogVo;
import com.tanhua.log.vo.PageResult;
import com.tanhua.log.vo.UserFreezingVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public PageResult getLoginLog(Integer pagesize, Integer page, String sortProp, String sortOrder, Integer uid) {


        Query query = Query.query(Criteria
                .where("logeType").is("/user/loginVerification")
                .and("userId").is(uid));
        //sortOrder	是
        //ascending 升序 descending 降序
        if (StringUtils.equalsIgnoreCase("ascending", sortOrder)) {
            query.with(PageRequest.of(page-1, pagesize, Sort.by(Sort.Order.asc("logTime"))));
        } else {
            query.with(PageRequest.of(page-1, pagesize, Sort.by(Sort.Order.desc("logTime"))));
        }

        List<Log> logs = mongoTemplate.find(query, Log.class);

        ArrayList<LoginLogVo> loginLogVos = new ArrayList<>();

        for (Log log : logs) {
            LoginLogVo loginLogVo = new LoginLogVo();

            loginLogVo.setId(uid);

            loginLogVo.setLogTime(log.getLogTime());
            loginLogVo.setPlace(log.getPlace());

            loginLogVos.add(loginLogVo);
        }

        PageResult pageResult = new PageResult();
        pageResult.setItems(loginLogVos);
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        int count = (int) mongoTemplate.count(query, Log.class);
        pageResult.setCounts(count);
        pageResult.setPages(count / pagesize);

        return pageResult;
    }



    public PageResult getLoginLogAll(Integer pagesize, Integer page, String sortProp, String sortOrder, Integer uid) {


        Query query = Query.query(Criteria
                .where("userId").is(uid));
        //sortOrder	是
        //ascending 升序 descending 降序
        if (StringUtils.equalsIgnoreCase("ascending", sortOrder)) {
            query.with(PageRequest.of(page-1, pagesize, Sort.by(Sort.Order.asc("logTime"))));
        } else {
            query.with(PageRequest.of(page-1, pagesize, Sort.by(Sort.Order.desc("logTime"))));
        }

        List<Log> logs = mongoTemplate.find(query, Log.class);

        ArrayList<LoginLogAllVo> loginLogVos = new ArrayList<>();

        for (Log log : logs) {
            LoginLogAllVo loginLogAllVo = new LoginLogAllVo();

            loginLogAllVo.setId(uid);
            loginLogAllVo.setLogType(log.getLogeType());
            loginLogAllVo.setLogTime(log.getLogTime());
            loginLogAllVo.setPlace(log.getPlace());

            loginLogVos.add(loginLogAllVo);
        }

        PageResult pageResult = new PageResult();
        pageResult.setItems(loginLogVos);
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        int count = (int) mongoTemplate.count(query, Log.class);
        pageResult.setCounts(count);
        pageResult.setPages(count / pagesize);

        return pageResult;
    }

    public PageResult getUserFreezing(Integer pagesize, Integer page, String sortProp, String sortOrder, Integer uid) {

        Query query = Query.query(Criteria.where("userId").is(uid)).with(Sort.by(Sort.Order.desc("logTime")));
        List<UserFreeze> freezeList = mongoTemplate.find(query, UserFreeze.class);

        ArrayList<UserFreezingVo> vos = new ArrayList<>();
        for (UserFreeze userFreeze : freezeList) {
            UserFreezingVo vo = new UserFreezingVo();
            vo.setId(userFreeze.getUserId());

            if (null!=userFreeze.getFreezingTime()){
                vo.setFreezingTime(userFreeze.getFreezingTime());
            }

            vo.setLogTime(userFreeze.getLogTime());
            Integer freezingRange = userFreeze.getFreezingRange();

            if (null!=freezingRange){
                String range = freezingRange.toString();
                if (StringUtils.isNotBlank(range)){
                    //1为冻结登录，2为冻结发言，3为冻结发布动态
                    if (StringUtils.equals(range,"1")){
                        vo.setFreezingRange("冻结登录");
                    }
                    if (StringUtils.equals(range,"2")){
                        vo.setFreezingRange("冻结发言");
                    }
                    if (StringUtils.equals(range,"3")){
                        vo.setFreezingRange("冻结发布动态");
                    }
                }
            }


            String remarks = userFreeze.getFrozenRemarks();
            if (StringUtils.isNotBlank(remarks)){
                vo.setFreezingRemarks(remarks);
                vo.setType("冻结");
            }

            String reasons = userFreeze.getReasonsForFreezing();
            if (StringUtils.isNotBlank(reasons)){
                vo.setFreezingReason(reasons);
            }

            String thawing = userFreeze.getReasonsForThawing();
            if (StringUtils.isNotBlank(thawing)){
                vo.setUnfreezingReason(thawing);
                vo.setType("解冻");

            }

            vos.add(vo);
        }

        PageResult pageResult = new PageResult();
        pageResult.setItems(vos);
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        int count = (int) mongoTemplate.count(query, Log.class);
        pageResult.setCounts(count);
        pageResult.setPages(count / pagesize);

        return pageResult;
    }

    /*
    * Due to limitations of the com.mongodb.BasicDocument,
    * you can't add a second 'logeType' expression specified
    * as 'logeType : login'. Criteria already contains 'logeType : login'.*/

}
